//
//  ZCResetPwdVC.m
//  ZCJZ
//
//  Created by sujeking on 2019/3/26.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "WZGResetPwdVC.h"
#import "ZCRegsitVCInputCell.h"
#import "ZCRegsitVCSendCodeCell.h"
#import "ZCInputAndBtnCell.h"

#import "ZCLoginViewModel.h"
#import "UIImage+Extension.h"
#import <BlocksKit+UIKit.h>

@interface WZGResetPwdVC ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *donebtn;

@property (nonatomic, strong) NSMutableDictionary *mdict;

@end

@implementation WZGResetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageFromColor:[UIColor whiteColor]]
                                                  forBarMetrics:UIBarMetricsDefault];
    
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorFromRGB(0x333333),
                                                                    NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                                                                    };
    UIImage *im = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e618", 25, UIColorFromRGB(0x333333))];
    [self setLeftItemWithIcon:im action:@selector(basePopAction)];
    

    self.navTitle = @"重置密码";
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCRegsitVCInputCell" bundle:nil]
         forCellReuseIdentifier:@"ZCRegsitVCInputCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCRegsitVCSendCodeCell" bundle:nil]
         forCellReuseIdentifier:@"ZCRegsitVCSendCodeCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCInputAndBtnCell" bundle:nil]
         forCellReuseIdentifier:@"ZCInputAndBtnCell"];
    
    self.donebtn.layer.cornerRadius = 5.0f;
    self.donebtn.layer.masksToBounds = YES;
    self.donebtn.backgroundColor = [generalToolBox getColorWithHexadecimalColorStr:@"0xF9150F"];
    
    
    [self.donebtn bk_addEventHandler:^(id sender) {
        [self.view endEditing:YES];
        if ([self.mdict valueForKey:@"nickname"] == nil) {
            [self.view makeToast:@"请输入用户名" duration:2.0 position:CSToastPositionCenter];
            return;
        }

        if ([self.mdict valueForKey:@"code"] == nil) {
            [self.view makeToast:@"请输入验证码" duration:2.0 position:CSToastPositionCenter];
            return;
        }

        NSString *pwd = [self.mdict valueForKey:@"pwd"];
        
        if (pwd == nil) {
            [self.view makeToast:@"请输入新密码" duration:2.0 position:CSToastPositionCenter];
            return;
        }
        
        if (pwd.length < 8) {
            [self.view makeToast:@"密码长度不够,8至20位数字或字母" duration:2.0 position:CSToastPositionCenter];
            return;
        }
        

        [self netCommitInfo2ResetPasswd];
    } forControlEvents:UIControlEventTouchUpInside];
}

// MARK: - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    NSInteger row = indexPath.row;
    switch (row) {
        case 0:
        {
            ZCRegsitVCInputCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCRegsitVCInputCell"
                                                                        forIndexPath:indexPath];
            cell.placeholder = @"请输入用户名";
            [cell setEndInputCallback:^(NSString * _Nonnull txt) {
                if (txt != nil && txt.length != 0) {
                    [weakSelf.mdict setValue:txt forKey:@"nickname"];
                }
            }];
            return cell;
        }
            break;
        case 1:
        {
            
            ZCRegsitVCSendCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCRegsitVCSendCodeCell"
                                                                           forIndexPath:indexPath];
            [cell setBtnClickCallback:^(NSString * _Nonnull txt) {
                if (txt == nil || txt.length == 0) {
                    [weakSelf.view makeToast:@"请输入手机号" duration:2.0 position:CSToastPositionCenter];
                } else {
                    [weakSelf.mdict setValue:txt forKey:@"phone"];
                    [weakSelf netSendVirfiyCodeWithPhoneNum:txt];
                }
            }];
            return cell;
            
        }
            break;
        case 2:
        {
            ZCRegsitVCInputCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCRegsitVCInputCell"
                                                                        forIndexPath:indexPath];
            cell.placeholder = @"请输入验证码";
            [cell setEndInputCallback:^(NSString * _Nonnull txt) {
                if (txt != nil && txt.length != 0) {
                    [weakSelf.mdict setValue:txt forKey:@"code"];
                }
            }];
            return cell;
        }
            break;
        case 3:
        {
            ZCInputAndBtnCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCInputAndBtnCell"
                                                                      forIndexPath:indexPath];
            cell.placeholder = @"请输入新密码";
            cell.secureTextEntry = YES;
            UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e646", 25, UIColorFromRGB(0x999999))];
            [cell.btn setImage:img forState:UIControlStateNormal];
            [cell setEndEditCallback:^(NSString * _Nonnull txt) {
                if (txt != nil && txt.length != 0) {
                    [weakSelf.mdict setValue:txt forKey:@"pwd"];
                }
            }];
            return cell;
        }
            break;
        default:
            break;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

// MARK: - NET

- (void)netSendVirfiyCodeWithPhoneNum:(NSString *)phoneNum {
    __weak typeof(self) weakSelf = self;
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [ZCLoginViewModel sendVirfiyCodeWithPhoneNum:phoneNum type:4 complete:^(ZCErrorCode status, NSString *message, id data) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        });
        if (status == kHXSNoError) {
            [weakSelf.view makeToast:@"验证码已发送" duration:2.0 position:CSToastPositionCenter];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"registsendcode" object:nil];
        } else {
            [weakSelf.view makeToast:message duration:2.0 position:CSToastPositionCenter];
        }
    }];
}

- (void)netCommitInfo2ResetPasswd {
    __weak typeof(self) weakSelf = self;
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [ZCLoginViewModel resetPasswordWithInfo:self.mdict complete:^(ZCErrorCode status, NSString *message, id data) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        });
        if (status == kHXSNoError) {
            [weakSelf.view makeToast:message duration:2.0 position:CSToastPositionCenter];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        } else {
            [weakSelf.view makeToast:message duration:2.0 position:CSToastPositionCenter];
        }
    }];
}

// MARK: - GET

- (NSMutableDictionary *)mdict {
    if (_mdict == nil) {
        _mdict = [NSMutableDictionary new];
    }
    return _mdict;
}

@end
